$(function () {
    H_login = {};
    H_login.openLogin = function () {
        $('#onload-login').click(function () {
            $('.login').show();
            $('.login-bg').show();
        });
    };
    H_login.closeLogin = function () {
        $('.close-login').click(function () {
            $('.login').hide();
            $('.login-bg').hide();
        });
    };
    H_login.loginForm = function () {
        $("#login-button-submit").on('click', function () {
            var username = $("#username");
            var usernameValue = $("#username").val();
            var password = $("#password");
            var passwordValue = $("#password").val();
            if (usernameValue == "") {
                alert("用户名不能为空");
                username.focus();
                return false;
            } else if (usernameValue.length > 15) {
                alert("用户名长度不能大于15字符");
                username.focus();
                return false;
            } else if (passwordValue == "") {
                alert("密码不能为空");
                password.focus();
                return false;
            } else if (passwordValue.length < 6 || passwordValue.length > 30) {
                alert("密码长度不能小于6或大于30位字符");
                password.focus();
                return false;
            } else {
                $.ajax({
                    async: false,
                    type: "POST",
                    data: {"code": $("#code").val()},
                    url: "/checkCode",
                    success: function (code_result) {
                        var val=$('input:radio[name="remeberMe"]:checked').val();
                        let r = 0;
                        if (val == null){
                            r = 0;
                        }else {
                            r = 1;
                        }
                        if (code_result == "success") {
                            $.ajax({
                                async: false,
                                type: "POST",
                                data: {"uPhone": usernameValue, "uEmail": usernameValue, "uPass": passwordValue,"remeberMe":r},
                                url: "/user/login",
                                success: function (login_result) {
                                    if (login_result == "success") {
                                        alert("登录成功");
                                        setTimeout(function () {
                                            $('.login').hide();
                                            $('.login-bg').hide();
                                            $('.list-input').val('');
                                        }, 2000);
                                        window.location.href="/";
                                    } else {
                                        alert("账号密码错误");
                                    }
                                }
                            });
                        } else {
                            alert("验证码错误");
                            getVerify();
                        }
                    }
                });
            }
        });
    };
    H_login.run = function () {
        this.closeLogin();
        this.openLogin();
        this.loginForm();
    };
    H_login.run();
});

